#define  _CRT_SECURE_NO_WARNINGS 1
#include "string.h"

namespace wwp
{
	//const static size_t npos=-1;
	string::string(const char* str)
		:_size(strlen(str))
	{
		_capacity = _size;
		_str = new char[_size + 1];
		strcpy(_str, str);
	}

	void string:: push_back(char ch)
	{
		if (_size + 1 > _capacity)
		{
			int newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			_capacity = newcapacity;
			char*str = new char[_capacity];
			strcpy(str, _str);
			string::~string();
			_str = str;
		}
		_str[_size] = ch;
		_size++;
		_str[_size] ='\0';
	}

	void string::append(const char* str)
	{
		assert(str);
		int len=strlen(str);
		if (_size + len > _capacity)
		{
			int newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			if (newcapacity < _size + len)
				newcapacity = _size + len + 1;

			_capacity = newcapacity;
			char* str = new char[_capacity];
			strcpy(str, _str);
			string::~string();
			_str = str;
		}
		for (int i = 0; i < len; i++)
		{
			_str[_size++] = *(str + i);
		}
		_str[_size] = '\0';
	}

	void string::insert(int pos, int n, char ch)
	{
		assert(pos >= 0 && n > 0);
		if (_size + n > _capacity)
		{
			int newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			if (newcapacity < _size +n)
				newcapacity = _size + n + 1;

			_capacity = newcapacity;
			char* str = new char[_capacity];
			strcpy(str, _str);
			string::~string();
			_str = str;
		}
		int end = _size + n;
		for (int i = _size; i >= pos; i--)
		{
			_str[end] = _str[i];
			end--;
		}
		for (int i = 0; i < n; i++)
		{
			_str[pos++] = ch;;
		}

	}

	void string::insert(int pos, const char* str)
	{
		assert(pos >= 0 && str);
		int len = strlen(str);
		if (_size + len > _capacity)
		{
			int newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			if (newcapacity < _size + len)
				newcapacity = _size + len + 1;

			_capacity = newcapacity;
			char* str = new char[_capacity];
			strcpy(str, _str);
			string::~string();
			_str = str;
		}
		int end = _size + len;
		for (int i = _size; i >= pos; i--)
		{
			_str[end] = _str[i];
			end--;
		}
		for (int i =0; i < len; i++)
		{
			_str[pos++] = *(str + i);
		}
	}

	void string::erase(int pos ,int  len )
	{
		assert(pos >= 0 && len > 0);
		if (len >=(_size - pos))
			_str[pos] = '\0';
		else
		{
			int end = pos + len;
			for (int i =end; i <=_size; i++)
			{
				_str[pos++] = _str[i];
			}
		}

	}

	string& string::operator+=(char ch)
	{
		string::push_back(ch);
		return *this;
	}

	string& string::operator+=(const char* str)
	{
		string::append(str);
		return *this;
	}
	void test()
	{
		//string s1("hello world");
		//string s1;
		string s1("hello world");
		cout << s1.c_str() << endl;
	/*	s1.push_back('x');
		cout << s1.c_str() << endl;*/
	/*	s1.append("xx");
		cout << s1.c_str() << endl;*/
		//s1.insert(0, 2, 'x');
		//s1.insert(10, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		/*s1.erase();
		cout << s1.c_str() << endl;*/

	/*	s1.erase(1,2);
		cout << s1.c_str() << endl;*/

		//s1.erase(10,2);
		//s1 += 'x';
		s1 += "xxxxx";
		cout << s1.c_str() << endl;
	}
}